package com.usercon.userconter.controller;

import com.usercon.userconter.Utils.MD5Util;
import com.usercon.userconter.Utils.ResultAll.Result;
import com.usercon.userconter.Utils.ResultAll.ResultUtils;
import com.usercon.userconter.Utils.ResultBll.Msg;
import com.usercon.userconter.Utils.ResultBll.ResultUtilt;
import com.usercon.userconter.pojo.User;
import com.usercon.userconter.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin(value = "*",maxAge = 3600)
@RestController
@RequestMapping("/api")
public class UserController {

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

//    @GetMapping
//    public List<User> findAll(){
//        System.out.println("UserController findAll已调用 返回值:" + userService.findAll());
//        return userService.findAll();
//    }
//    @GetMapping
//    public Msg findAll(){
//        System.out.println("UserController findAll已调用 返回值:" + userService.findAll());
//        return ResultUtilt.success(userService.findAll());
//    }

    @GetMapping
    public Result findAll(){
        System.out.println("UserController findAll已调用 返回值:" + userService.findAll());
        return ResultUtils.success(userService.findAll(),"成功");
    }

//    @GetMapping("/findById")
//    public User findById(@RequestParam("id") int id){
//        return userService.findById(id);
//    }
    @GetMapping("/findById")
    public Msg findById(@RequestParam("id") int id){
        return ResultUtilt.success(userService.findById(id));
    }

    @PostMapping
    public int addUser(int id,String username,String password){
        String md5pass = MD5Util.MD5Encode(password,"utf-8");
        int addUser = userService.addUser(id,username,md5pass);
        if(addUser > 0) {
            return addUser;
        }else {
            System.out.println("返回错误");
            return 500;
        }
    }

    @PutMapping
    public int updateUser(User user){
        int updateUser = userService.updateUser(user);
        if(updateUser > 0){
            return updateUser;
        }else{
            return 500;
        }
    }

    @DeleteMapping
    public Integer deleteUser(@RequestParam("id") int id){
        return userService.deleteUser(id);
    }

}
